//
//  AuthHelper.m
//  WindRunner
//
//  Created by leeqb on 11/4/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "AuthHelper.h"
#import "BaseTabBarController.h"

#define CURRENT_USER_INFO   @"CurrentUserInfo"

void (^g_auth_success)(NSDictionary *userInfo);
void (^g_auth_failed)(NSError *error);

@implementation AuthHelper

+ (void)releaseBlocks
{
    g_auth_success = nil;
    g_auth_failed = nil;
}

// 有用户信息表示已登录，无用户信息表示未登录
+ (BOOL)isLogin
{
    if([AuthHelper getCurrentUser]) {
        return YES;
    } else {
        return NO;
    }
}

+ (NSDictionary *)getCurrentUser
{
    NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:CURRENT_USER_INFO];
    if(userInfo && userInfo.allKeys.count > 0) {
        return userInfo;
    } else {
        return nil;
    }
}

+ (void)setCurrentUser:(NSDictionary *)userInfo
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userInfo forKey:CURRENT_USER_INFO];
    [userDefaults synchronize];
}

+ (void)checkLogin:(void (^)(NSDictionary *userInfo))success failed:(void (^)(NSError *error))failed
{
    g_auth_success = success;
    g_auth_failed = failed;
    
    if([AuthHelper isLogin]) {
        g_auth_success([AuthHelper getCurrentUser]);
        [self releaseBlocks];
    } else {
        g_auth_failed([NSError errorWithDomain:@"Not login" code:-1 userInfo:nil]);
        /*UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Security" bundle:nil];
        LoginConsoleViewController *vc = (LoginConsoleViewController *)[storyboard instantiateInitialViewController];
        
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        UINavigationController *rootNav = (UINavigationController *)keyWindow.rootViewController;
        [rootNav pushViewController:vc animated:YES];*/
    }
}

+ (void)onLoginSuccess:(NSDictionary *)userInfo
{
    if(g_auth_success) {
        NSDictionary *record = userInfo[@"record"];
        if(record && ![record isEqual:[NSNull null]]) {
            NSMutableDictionary *temp = [NSMutableDictionary dictionary];
            for(NSString *key in record.allKeys) {
                if([record[key] isEqual:[NSNull null]]) {
                    [temp setValue:@"" forKey:key];
                } else {
                    [temp setValue:record[key] forKey:key];
                }
            }
            
            [AuthHelper setCurrentUser:temp];
            g_auth_success(temp);
        } else if(g_auth_failed) {
            g_auth_failed([NSError errorWithDomain:@"Auth failed,no record" code:-1 userInfo:nil]);
        }
    } else if(g_auth_failed) {
        g_auth_failed([NSError errorWithDomain:@"Auth failed,login failed" code:-1 userInfo:nil]);
    }
    
    /*
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UINavigationController *rootNav = (UINavigationController *)keyWindow.rootViewController;
    rootNav.navigationBarHidden = YES;
    [rootNav popToRootViewControllerAnimated:YES];
    [self releaseBlocks];
     */
}

+ (void)onLoginFailed:(NSError *)error
{
    if(g_auth_failed) {
        g_auth_failed(error);
        g_auth_failed = nil;
    }
}

+ (void)logout
{
    // 用户信息置为空表示从风行者后台注销
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:nil forKey:CURRENT_USER_INFO];
    [userDefaults synchronize];
    
    // 环信注销
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES];
}

@end
